#include "BlocksMatrix.h"
#include "Rectangle.h"
#include "link.h"
#include <time.h>
#include "GameFont.h"
#include <sstream>
#include "ComboControl.h"
using namespace std;

CBlocksMatrix::CBlocksMatrix(CMatrixEventsListener* pListener, int xPos, int yPos) 
  : m_iXPos(xPos), m_iYPos(yPos),
    m_bRemovingLine(false), m_iLineBlinkCount(0), m_bLineBlinkOn(false), 
	m_vecLinesRemoved(), m_pListener(pListener), m_iTetradUpdate(1000),
	m_bGameOver(false)
{
	trung_gam[0] = CImage::CreateImage("trung.PNG",TRectanglei(0,BLOCK_HEIGHT,0,BLOCK_WIDTH));
	trung_gam[1] = CImage::CreateImage("chatthai.PNG",TRectanglei(0,BLOCK_HEIGHT,0,BLOCK_WIDTH));
	trung_vitm[0] = CImage::CreateImage("trung.PNG",TRectanglei(0,BLOCK_HEIGHT,0,BLOCK_WIDTH));
	trung_vitm[1] = CImage::CreateImage("chatthai.PNG",TRectanglei(0,BLOCK_HEIGHT,0,BLOCK_WIDTH));
	trung_nganm[0] = CImage::CreateImage("trung.PNG",TRectanglei(0,BLOCK_HEIGHT,0,BLOCK_WIDTH));
	trung_nganm[1] = CImage::CreateImage("chatthai.PNG",TRectanglei(0,BLOCK_HEIGHT,0,BLOCK_WIDTH));
	conngan[0] = CImage::CreateImage("ngan.PNG",TRectanglei(0,vat_cao,0,vat_rong));
	conga[0] = CImage::CreateImage("ga.PNG",TRectanglei(0,vat_cao,0,vat_rong));
	convit[0] = CImage::CreateImage("vit.PNG",TRectanglei(0,vat_cao,0,vat_rong));
	cairo[0] = CImage::CreateImage("ro.PNG",TRectanglei(0,cairo_cao,0,cairo_rong));
	//tao ma tran kiem tra trung
		for (int j=0; j<15;j++)
		{
			if(j==7||j==8||j==9)
			{
				matran[j]=1;
			}
			matran[j] = bcNone;
		}
		//khoi tao cac gia tri ban dau
	 tocdo=0;
	capdo=1;
	sotrunghunh=0;
	sotrungout=0;
	bathung=0;
	tangtoc=0;
	 kh=2;
	 ko=2;
	n1=false;
	n2=false;
	kn=false;
	 v1=false;  
	 v2=false; 
	 checkv=false;
	 g1=false;  
	 g2=false; 
	 checkg=false;
	 setngan=0;
	  setngan1=0;
	 setvit=0;
	 setvit1=0;
	 setga=0;
	 setga1=0;
	 //khoi tao du lieu mac dinh cho vat
	trung_ga1 = m_TetradFactory.CreateTetrad(this);
	trung_ga1doi = m_TetradFactory.CreateTetrad(this);
	trung_vit1 = m_TetradFactory.CreateTetrad(this);
	trung_vit1doi = m_TetradFactory.CreateTetrad(this);
	tro = m_TetradFactory.taongana(this);
	tro->co=false;
	tro->thoigian=0;
	
	ngan = m_TetradFactory.taongana(this);
	troga = m_TetradFactory.taoga(this);
	ga = m_TetradFactory.taoga(this);
	trovit = m_TetradFactory.taovit(this);
	vit = m_TetradFactory.taovit(this);
	ro = m_TetradFactory.taoro(this);
	trung_ngan1 = m_TetradFactory.CreateTetrad(this);
	trung_ngan1doi = m_TetradFactory.CreateTetrad(this);
	trung_ngan2 = m_TetradFactory.CreateTetrad(this);
	trung_ngan2doi = m_TetradFactory.CreateTetrad(this);
	trung_ga1 = m_TetradFactory.CreateTetrad(this);
	trung_ga1doi = m_TetradFactory.CreateTetrad(this);
	trung_ga2 = m_TetradFactory.CreateTetrad(this);
	trung_ga2doi = m_TetradFactory.CreateTetrad(this);
	trung_vit1 = m_TetradFactory.CreateTetrad(this);
	trung_vit1doi = m_TetradFactory.CreateTetrad(this);
	trung_vit2 = m_TetradFactory.CreateTetrad(this);
	trung_vit2doi = m_TetradFactory.CreateTetrad(this);

	m_dwLastShapeDown = GetCurrentTime();
}

CBlocksMatrix::~CBlocksMatrix()
{
}

void CBlocksMatrix::Reset()
{
	for (int i=0; i<MATRIX_WIDTH;i++)
	{
		for (int j=0; j<MATRIX_HEIGHT;j++)
		{
			m_pBlocksMatrix[i][j] = bcNone;
		}
	}
	m_dwLastShapeDown = GetCurrentTime();
    m_bRemovingLine = m_bLineBlinkOn = false; 
	m_iLineBlinkCount = 0;
	m_vecLinesRemoved.clear();
	m_iTetradUpdate = 1000;
	m_bGameOver = false;
}

void CBlocksMatrix::Draw()
{
	int iBlockX=10, iBlockY=10;

	if (m_bRemovingLine)
	{
		for (int j=0; j<MATRIX_HEIGHT;j++)
		{
	
			if (IsLineRemoved(j) && !m_bLineBlinkOn)
				continue;

			for (int i=0; i<MATRIX_WIDTH;i++)
			{
				if (m_pBlocksMatrix[i][j])
				{
					
					GetScreenPosFromCell(i, j, iBlockX, iBlockY);
				}			
			}
		}

		if (m_bLineBlinkOn)
			m_bLineBlinkOn = false;
		else
			m_bLineBlinkOn = true;
		m_iLineBlinkCount++;
	
		if (m_iLineBlinkCount == 10)
		{
			RemoveLines();
			m_bRemovingLine = false;
			m_bLineBlinkOn = false;
			m_iLineBlinkCount = 0;
			NewShape();
		}
	}
	else 
	{

		for (int j=0; j<MATRIX_HEIGHT;j++)
		{
			for (int i=0; i<MATRIX_WIDTH;i++)
			{
				if (m_pBlocksMatrix[i][j])
				{
					
					GetScreenPosFromCell(i, j, iBlockX, iBlockY);
				}
			}
		}

	if (!m_bGameOver)
	{
		ro->Draw();
		tro->Draw();
	if(n1==true)
	{
    ngan->Draw();
	}
	if(v1==true)
	{
    trovit->Draw();
	}
	if(v2==true)
	{
    vit->Draw();
	}
	if(g1==true)
	{
		troga->Draw();
	}
	if(g2==true)
	{
		ga->Draw();
	}
	if(trung_ngan1->co==false)
		trung_ngan1->Draw();
	if(trung_ngan1doi->co==false)
		trung_ngan1doi->Draw();
	if(trung_ngan2->co==false)
		trung_ngan2->Draw();
	if(trung_ngan2doi->co==false)
		trung_ngan2doi->Draw();
	if(trung_ga1->co==false)
		trung_ga1->Draw();
	if(trung_ga1doi->co==false)
		trung_ga1doi->Draw();
	if(trung_ga2->co==false)
		trung_ga2->Draw();
	if(trung_ga2doi->co==false)
		trung_ga2doi->Draw();
	if(trung_vit1->co==false)
		trung_vit1->Draw();
	if(trung_vit1doi->co==false)
		trung_vit1doi->Draw();
	if(trung_vit2->co==false)
		trung_vit2->Draw();
	if(trung_vit2doi->co==false)
		trung_vit2doi->Draw();
	}
	}

}
//cap nhap doi tuong trung roi

void CBlocksMatrix::Update(DWORD dwCurrentTime)
{
	if(bathung==3 || sotrungout==3)
	{
		//game over
	}
	if(sotrunghunh==20  || sotrunghunh==40 ||sotrunghunh==60 ||sotrunghunh==80)
	{
		tangtoc++;
	}
	//CGameFont* m_pFont;
	//m_pFont = new CGameFont;
//	m_pFont->CreateFont("Verdana", 30, FW_NORMAL);
	//stringstream l;
	//l << "Cap do: " << tangtoc;
	//m_pFont->DrawText(l.str(),10, 30,1.0f,0,0);
	//stringstream ssText;
	//ssText << "So luong trung: " << sotrunghunh;
	//m_pFont->DrawText(ssText.str(),10, 60,1.0f,0,0);
	//if (!m_bGameOver)
	{

		if (dwCurrentTime > m_dwLastShapeDown+m_iTetradUpdate)
		{
				// xu ly tha trung cho tung con vat
	if(tro->co==false || ngan->co==false || troga->co==false || ga->co==false || trovit->co==false
		|| vit->co==false)
	{
		if(tro->co==false)
	{
		if(tro->thoigian==0)
		{
			
			if(setngan==0)
			{
				trung_ngan1->co=false;
				trung_ngan1->m_iXPos=tro->m_iXPos;
				trung_ngan1->m_iYPos=1;
				setngan=1;
			}
			if(setngan==2)
			{
				trung_ngan1doi->co=false;
				trung_ngan1doi->m_iXPos=tro->m_iXPos;
				trung_ngan1doi->m_iYPos=1;
				setngan=0;
			}

			//tao ngau nhien vat roi
		}
		if(tro->thoigian==5)
		{
			if(setngan==1)
			setngan=2;
			tro->thoigian=0;
		}
		else
		{
		tro->thoigian++;
		}
		if(trung_ngan1->m_iYPos>12)
	{
		if(matran[trung_ngan1->m_iXPos]==1)
		{

			if(trung_ngan1->settortr==1)
			{
				sotrunghunh++;
			}
			else
			{
				sotrungout++;
			}
		}
		delete trung_ngan1;
		trung_ngan1 = m_TetradFactory.CreateTetrad(this);
		trung_ngan1->co=true;
	}
	else
	{
			trung_ngan1->MoveDown();
	}
	if(trung_ngan1doi->m_iYPos>12)
	{
		if(matran[trung_ngan1doi->m_iXPos]==1)
		{

			if(trung_ngan1doi->settortr==1)
			{
				sotrunghunh++;
			}
			else
			{
				sotrungout++;
			}
		}
		else
		{
			bathung++;
		}
			delete trung_ngan1doi;
			trung_ngan1doi = m_TetradFactory.CreateTetrad(this);
			trung_ngan1doi->co=true;
	}
	else
	{
			trung_ngan1doi->MoveDown();
	}
	}
		//xu ly trung roi cua ngan2
		if(ngan->co==false)
	{
		if(ngan->thoigian==0)
		{
			
			if(setngan1==0)
			{
				trung_ngan2->co=false;
				trung_ngan2->m_iXPos=ngan->m_iXPos;
				trung_ngan2->m_iYPos=1;
				setngan1=1;
			}
			if(setngan1==2)
			{
				trung_ngan2doi->co=false;
				trung_ngan2doi->m_iXPos=ngan->m_iXPos;
				trung_ngan2doi->m_iYPos=1;
				setngan1=0;
			}

			//tao ngau nhien vat roi
		}
		if(ngan->thoigian==5)
		{
			if(setngan1==1)
			setngan1=2;
			ngan->thoigian=0;
		}
		else
		{
		ngan->thoigian++;
		}
		if(trung_ngan2->m_iYPos>12)
	{
		if(matran[trung_ngan2->m_iXPos]==1)
		{

			if(trung_ngan2->settortr==1)
			{
				sotrunghunh++;
			}
			else
			{
				sotrungout++;
			}
		}
		else
		{
			bathung++;
		}
		delete trung_ngan2;
		trung_ngan2 = m_TetradFactory.CreateTetrad(this);
		trung_ngan2->co=true;
	}
	else
	{
			trung_ngan2->MoveDown();
	}
	if(trung_ngan2doi->m_iYPos>12)
	{
		if(matran[trung_ngan2doi->m_iXPos]==1)
		{

			if(trung_ngan2doi->settortr==1)
			{
				sotrunghunh++;
			}
			else
			{
				sotrungout++;
			}
		}
		else
		{
			bathung++;
		}	
		delete trung_ngan2doi;
			trung_ngan2doi = m_TetradFactory.CreateTetrad(this);
			trung_ngan2doi->co=true;
	}
	else
	{
			trung_ngan2doi->MoveDown();
	}
	}
		//xu ly tra trung cua ga 1
		if(ga->co==false)
	{
		if(ga->thoigian==0)
		{
			
			if(setga==0)
			{
				trung_ga1->co=false;
				trung_ga1->m_iXPos=ga->m_iXPos;
				trung_ga1->m_iYPos=1;
				setga=1;
			}
			if(setga==2)
			{
				trung_ga1doi->co=false;
				trung_ga1doi->m_iXPos=ga->m_iXPos;
				trung_ga1doi->m_iYPos=1;
				setga=0;
			}

			//tao ngau nhien vat roi
		}
		if(ga->thoigian==5)
		{
			if(setga==1)
			setga=2;
			ga->thoigian=0;
		}
		else
		{
		ga->thoigian++;
		}
		if(trung_ga1->m_iYPos>12)
	{
		if(matran[trung_ga1->m_iXPos]==1)
		{

			if(trung_ga1->settortr==1)
			{
				sotrunghunh++;
			}
			else
			{
				sotrungout++;
			}
		}
		else
		{
			bathung++;
		}
		delete trung_ga1;
		trung_ga1 = m_TetradFactory.CreateTetrad(this);
		trung_ga1->co=true;
	}
	else
	{
			trung_ga1->MoveDown();
	}
	if(trung_ga1doi->m_iYPos>12)
	{
		if(matran[trung_ga1doi->m_iXPos]==1)
		{

			if(trung_ga1doi->settortr==1)
			{
				sotrunghunh++;
			}
			else
			{
				sotrungout++;
			}
		}
		else
		{
			bathung++;
		}
			delete trung_ga1doi;
			trung_ga1doi = m_TetradFactory.CreateTetrad(this);
			trung_ga1doi->co=true;
	}
	else
	{
			trung_ga1doi->MoveDown();
	}
	}
		//xu ly trung ga 2
		if(troga->co==false)
	{
		if(troga->thoigian==0)
		{
			
			if(setga1==0)
			{
				trung_ga2->co=false;
				trung_ga2->m_iXPos=troga->m_iXPos;
				trung_ga2->m_iYPos=1;
				setga1=1;
			}
			if(setga1==2)
			{
				trung_ga2doi->co=false;
				trung_ga2doi->m_iXPos=troga->m_iXPos;
				trung_ga2doi->m_iYPos=1;
				setga1=0;
			}

			//tao ngau nhien vat roi
		}
		if(troga->thoigian==5)
		{
			if(setga1==1)
			setga1=2;
			troga->thoigian=0;
		}
		else
		{
		troga->thoigian++;
		}
		if(trung_ga2->m_iYPos>12)
	{
		if(matran[trung_ga2->m_iXPos]==1)
		{

			if(trung_ga2->settortr==1)
			{
				sotrunghunh++;
			}
			else
			{
				sotrungout++;
			}
		}
		else
		{
			bathung++;
		}
		delete trung_ga2;
		trung_ga2 = m_TetradFactory.CreateTetrad(this);
		trung_ga2->co=true;
	}
	else
	{
			trung_ga2->MoveDown();
	}
	if(trung_ga2doi->m_iYPos>12)
	{
		if(matran[trung_ga2doi->m_iXPos]==1)
		{

			if(trung_ga2doi->settortr==1)
			{
				sotrunghunh++;
			}
			else
			{
				sotrungout++;
			}
		}
		else
		{
			bathung++;
		}
			delete trung_ga2doi;
			trung_ga2doi = m_TetradFactory.CreateTetrad(this);
			trung_ga2doi->co=true;
	}
	else
	{
			trung_ga2doi->MoveDown();
	}
	}


		//xu ly vit
		if(vit->co==false)
			{
		if(vit->thoigian==0)
		{
			
			if(setvit==0)
			{
				trung_vit1->co=false;
				trung_vit1->m_iXPos=vit->m_iXPos;
				trung_vit1->m_iYPos=1;
				setvit=1;
			}
			if(setvit==2)
			{
				trung_vit1doi->co=false;
				trung_vit1doi->m_iXPos=vit->m_iXPos;
				trung_vit1doi->m_iYPos=1;
				setvit=0;
			}

			//tao ngau nhien vat roi
		}
		if(vit->thoigian==5)
		{
			if(setvit==1)
			setvit=2;
			vit->thoigian=0;
		}
		else
		{
		vit->thoigian++;
		}
		if(trung_vit1->m_iYPos>12)
	{
		if(matran[trung_vit1->m_iXPos]==1)
		{

			if(trung_vit1->settortr==1)
			{
				sotrunghunh++;
			}
			else
			{
				sotrungout++;
			}
		}
		else
		{
			bathung++;
		}
		delete trung_vit1;
		trung_vit1 = m_TetradFactory.CreateTetrad(this);
		trung_vit1->co=true;
	}
	else
	{
			trung_vit1->MoveDown();
	}
	if(trung_vit1doi->m_iYPos>12)
	{
		if(matran[trung_vit1doi->m_iXPos]==1)
		{

			if(trung_vit1doi->settortr==1)
			{
				sotrunghunh++;
			}
			else
			{
				sotrungout++;
			}
		}
		else
		{
			bathung++;
		}
			delete trung_vit1doi;
			trung_vit1doi = m_TetradFactory.CreateTetrad(this);
			trung_vit1doi->co=true;
	}
	else
	{
			trung_vit1doi->MoveDown();
	}
	}

		//xu ly vit 2 

		if(trovit->co==false)
	{
		if(trovit->thoigian==0)
		{
			
			if(setvit1==0)
			{
				trung_vit2->co=false;
				trung_vit2->m_iXPos=trovit->m_iXPos;
				trung_vit2->m_iYPos=1;
				setvit1=1;
			}
			if(setvit1==2)
			{
				trung_vit2doi->co=false;
				trung_vit2doi->m_iXPos=trovit->m_iXPos;
				trung_vit2doi->m_iYPos=1;
				setga1=0;
			}

			//tao ngau nhien vat roi
		}
		if(trovit->thoigian==5)
		{
			if(setvit1==1)
			setvit1=2;
			trovit->thoigian=0;
		}
		else
		{
		trovit->thoigian++;
		}
		if(trung_vit2->m_iYPos>12)
	{
		if(matran[trung_vit2->m_iXPos]==1)
		{

			if(trung_vit2->settortr==1)
			{
				sotrunghunh++;
			}
			else
			{
				sotrungout++;
			}
		}
		else
		{
			bathung++;
		}
		delete trung_vit2;
		trung_vit2 = m_TetradFactory.CreateTetrad(this);
		trung_vit2->co=true;
	}
	else
	{
			trung_vit2->MoveDown();
	}
	if(trung_vit2doi->m_iYPos>12)
	{
		if(matran[trung_vit2doi->m_iXPos]==1)
		{

			if(trung_vit2doi->settortr==1)
			{
				sotrunghunh++;
			}
			else
			{
				sotrungout++;
			}
		}
		else
		{
			bathung++;
		}

			delete trung_vit2doi;
			trung_vit2doi = m_TetradFactory.CreateTetrad(this);
			trung_vit2doi->co=true;
	}
	else
	{
			trung_vit2doi->MoveDown();
	}
	}

	}
	//trung di chuyen xuong
	//di chuyen ngan
			move_ngan();
			if(v1==true)
		{
//di chuyen vit
		move_vit();
		}
			if(g1==true)
		{
		move_ga();
		}
			//kiem tra su tao trung
			if(trung_ga1->m_iYPos>10 || trung_ga1doi->m_iYPos>10 || trung_vit1->m_iYPos>10 || trung_vit1doi->m_iYPos>10 || trung_ngan1->m_iYPos>10 || trung_ngan1doi->m_iYPos>10)
			{
				if(trung_ga1->m_iYPos>10)
				{
				//xoa hinh cu chem hinh moi
				}

			}
			// di chuyen cua con ngan thu 2
			if(n1==true)
			{
			if (( ngan->m_iXPos < 0) || ( ngan->m_iXPos>13))
				{
		if( ngan->m_iXPos < 0)
		{
			n2=false;
			ngan->MoveRight();
		}
		if( ngan->m_iXPos>13)
		{
			n2=true;
			ngan->MoveLeft();
		}
				}
		if(n2==true)
		{
			ngan->MoveLeft();
		}
		else
		{
			ngan->MoveRight();
		}
			}
		
		//di chuyen con vit thu 2
		if(v2==true)
			{
			if (( vit->m_iXPos < 0) || ( vit->m_iXPos>13))
				{
					if( vit->m_iXPos < 0)
						{
							checkv=false;
							vit->MoveRight();
						}
					if( vit->m_iXPos>13)
						{
							checkv=true;
							vit->MoveLeft();
						}
				}
			if(checkv==true)
				{
					vit->MoveLeft();
				}
			else
				{
					vit->MoveRight();
				}
			}
	//di chuyen co con ga thu 2
		if(g2==true)
			{
			if (( ga->m_iXPos < 0) || ( ga->m_iXPos>13))
				{
					if( ga->m_iXPos < 0)
						{
							checkg=false;
							ga->MoveRight();
						}
					if( ga->m_iXPos>13)
						{
							checkg=true;
							ga->MoveLeft();
						}
				}
			if(checkg==true)
				{
					ga->MoveLeft();
				}
			else
				{
					ga->MoveRight();
				}
			}
		}

	}
}

void CBlocksMatrix::ShapeLeft()
{
	if(ro->m_iXPos>-1)
	{
		for(int i=0;i<15;i++)
		{
			if(matran[i]==1)
			{
				matran[i-1]=1;
				matran[i+2]=0;
				i+=4;
			}
		}
	ro->MoveLeft();
	}
}

void CBlocksMatrix::ShapeRight()
{
	//su di chuyen qua phai cua ro
	if(ro->m_iXPos<14)
	{
		for(int i=0;i<15;i++)
		{
			if(matran[i]==1)
			{
				matran[i]=0;
				matran[i+2]=1;
				i+=4;
			}
		}
	ro->MoveRight();
	}
}

void CBlocksMatrix::move_ngan()
{
	if (( tro->m_iXPos < 0) || ( tro->m_iXPos>13))
	{
		if( tro->m_iXPos < 0)
		{
			kn=false;
			tro->MoveRight();
		}
		if( tro->m_iXPos>13)
		{
			kn=true;
			tro->MoveLeft();
		}
	}
		
		if(kn==true)
		{
			tro->MoveLeft();
		}
		else
		{
			tro->MoveRight();
		}
		m_dwLastShapeDown = GetCurrentTime();
}

void CBlocksMatrix::move_ga()
{
	if (( troga->m_iXPos < 0) || ( troga->m_iXPos>13))
	{
		if( troga->m_iXPos < 0)
		{
			checkg=false;
			troga->MoveRight();
		}
		if( troga->m_iXPos>13)
		{
			checkg=true;
			troga->MoveLeft();
		}
	}
		
		if(checkg==true)
		{
			troga->MoveLeft();
		}
		else
		{
			troga->MoveRight();
		}
	
}

void CBlocksMatrix::move_vit()
{
	if (( trovit->m_iXPos < 0) || ( trovit->m_iXPos>13))
	{
		if( trovit->m_iXPos < 0)
		{
			checkv=false;
			trovit->MoveRight();
		}
		if( trovit->m_iXPos>13)
		{
			checkv=true;
			trovit->MoveLeft();
		}
	}
		
		if(checkv==true)
		{
			trovit->MoveLeft();
		}
		else
		{
			trovit->MoveRight();
		}
}


void CBlocksMatrix::ShapeDown()
{
	//m_dwLastShapeDown = GetCurrentTime();
}

void CBlocksMatrix::NewShape()
{

}

void CBlocksMatrix::taotrungga()
{
	//trung duoc tao ngau nhien
	
}

void CBlocksMatrix::taotrungvit()
{
 
}

void CBlocksMatrix::taotrungngan()
{
}
void CBlocksMatrix::taongan()
{
	//chech tao vit
	n1=true;
	ngan->Draw();
	ngan->co=false;
	ngan->thoigian=0;
}
void CBlocksMatrix::taovit()
{
	//chech tao vit
	if(v1==true)
	{
		v2=true;
		vit->Draw();
		vit->co=false;
	vit->thoigian=0;
	}
	else
	{
	v1=true;
	trovit->Draw();
	trovit->co=false;
	trovit->thoigian=0;
	}
}
void CBlocksMatrix::taoga()
{    
	//check tao ga
	if(g1==true)
	{
		g2=true;
		ga->Draw();
		ga->co=false;
	ga->thoigian=0;
	}
	else
	{
	g1=true;
	troga->Draw();
	troga->co=false;
	troga->thoigian=0;
	}
}
void CBlocksMatrix::ShapeRotate()
{
	//if (m_pTetrad)
		//m_pTetrad->Rotate();
}
//Kiem tra ma tran 
bool CBlocksMatrix::IsCellFree(int XPos, int YPos)
{
	if ( (XPos<0) || (XPos>=MATRIX_WIDTH) )
		return false;
	if ( (YPos<0) || (YPos>=MATRIX_HEIGHT) )
		return false;

	if (m_pBlocksMatrix[XPos][YPos] == bcNone)
		return true;
	return false;
}

void CBlocksMatrix::FillCell(int XPos, int YPos, EBlockColor BlockColor)
{
	if ( (XPos<0) || (XPos>=MATRIX_WIDTH) )
		return;
	if ( (YPos<0) || (YPos>=MATRIX_HEIGHT) )
		return;

	m_pBlocksMatrix[XPos][YPos] = BlockColor;
}

void CBlocksMatrix::GetScreenPosFromCell(int cellXPos, 
										 int cellYPos, 
										 int& screenXPos, 
										 int& screenYPos)
{
	screenXPos = m_iXPos + BLOCK_WIDTH * cellXPos;	
	screenYPos = m_iYPos + BLOCK_HEIGHT * cellYPos;	
}

bool CBlocksMatrix::CheckMatrix()
{
	bool bOneLineComplete = false;
	bool bComplete = true;
	for (int iRow=0; iRow<MATRIX_HEIGHT;iRow++)
	{
		bComplete = true;
		for (int j=0; j<MATRIX_WIDTH;j++)
		{
			if (m_pBlocksMatrix[j][iRow] == bcNone)
			{
				bComplete = false;
				break;
			}
		}

		if (bComplete)
		{
			m_vecLinesRemoved.push_back(iRow);
			m_bRemovingLine = true;
			bOneLineComplete = true;
			m_pListener->OnStartRemoveLines();
		}
	}

	return bOneLineComplete;
}

bool CBlocksMatrix::IsLineRemoved(int iRow)
{
	bool bFound = false;
	vector<int>::iterator iter = m_vecLinesRemoved.begin();
	for (iter; iter!=m_vecLinesRemoved.end(); iter++)
	{
		if (iRow == (*iter) )
		{
			bFound = true;
			break;
		}
	}

	return bFound;
}

void CBlocksMatrix::RemoveLines()
{
	vector<int>::iterator iter = m_vecLinesRemoved.begin();
	for (iter; iter!=m_vecLinesRemoved.end();iter++)
	{
		for (int j=0; j<MATRIX_WIDTH;j++)
		{
		
			m_pBlocksMatrix[j][(*iter)] = bcNone;

			for (int iNewRow=(*iter);iNewRow>1;iNewRow--)
			{
				m_pBlocksMatrix[j][iNewRow] = m_pBlocksMatrix[j][iNewRow-1];
			}
		}
	}

	if (m_pListener)
		m_pListener->OnLinesRemoved((int)m_vecLinesRemoved.size());

	m_vecLinesRemoved.clear();
}